package web;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.validation.SkipValidation;
import org.apache.struts2.views.freemarker.tags.SetModel;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sun.org.apache.bcel.internal.generic.NEW;

import dao.ResourceDAO;
import domain.Resource;
import domain.util.MarcFormData;
import domain.util.TypesResource;
import facade.Facade;

public class ResourceAction extends ActionSupport implements
		ModelDriven<Resource> {

	private static final long serialVersionUID = 1L;

	private Resource resource = new Resource();
	private Facade facade = Facade.getInstance();
	private List<Resource> listArticle = new ArrayList<Resource>();
	private List<Resource> listIssue = new ArrayList<Resource>();
	public List<Resource> getListArticle() {
		return listArticle;
	}

	public void setListArticle(List<Resource> listArticle) {
		this.listArticle = listArticle;
	}

	public String goToSimple() {
		return SUCCESS;
	}
	
	private String add(){
		if (facade.addResource(resource))
			return SUCCESS;
		return ERROR;
	}
	
	public String addIssue() {
		resource.setType(TypesResource.Issue);
		return add();
	}
	
public String listIssue() {
		
	listIssue = facade.getResourcesBy("type", String.valueOf(TypesResource.Issue.ordinal()));
		
		return SUCCESS;
	}
	
	public List<Resource> getListIssue() {
	return listIssue;
}

public void setListIssue(List<Resource> listIssue) {
	this.listIssue = listIssue;
}

	public String showIssue() {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		setResource(facade.getResourcesById((Long.parseLong(request.getParameter("id")))));
		
		
		return SUCCESS;
	}
	
	public String editIssue() {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		setResource(facade.getResourcesById((Long.parseLong(request.getParameter("id")))));		
		
		return SUCCESS;
	}
	
	public String deleteIssue() {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		setResource(facade.getResourcesById((Long.parseLong(request.getParameter("id")))));
		
		
		return SUCCESS;
	}
	
	public String addCollectionArticle() {
		resource.setType(TypesResource.CollectionArticle);
		return add();
	}
	
	public String addBook() {
		resource.setType(TypesResource.Book);
		return add();
	}
	
	public String addSerial() {
		resource.setType(TypesResource.Serial);
		return add();
	}
	
	public String addTechnicalReport() {
		resource.setType(TypesResource.TechnicalReport);
		return add();
	}
	
	public String addThesis() {
		resource.setType(TypesResource.Thesis);
		return add();
	}
	
	public String addVolume() {
		resource.setType(TypesResource.Volume);
		return add();
	}

	public String addOther() {
		resource.setType(TypesResource.Other);
		return add();
	}
	
	public String addArticle() {
		resource.setType(TypesResource.Article);
		return add();
	}

	public String listArticle() {
		
		listArticle = facade.getResourcesBy("type", String.valueOf(TypesResource.Article.ordinal()));
		
		return SUCCESS;
	}
	
	public String showArticle() {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		setResource(facade.getResourcesById((Long.parseLong(request.getParameter("id")))));
		
		
		return SUCCESS;
	}
	
	public String editArticle() {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		setResource(facade.getResourcesById((Long.parseLong(request.getParameter("id")))));		
		
		return SUCCESS;
	}
	
	public String deleteArticle() {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		setResource(facade.getResourcesById((Long.parseLong(request.getParameter("id")))));
		
		
		return SUCCESS;
	}
	
	@Override
	public Resource getModel() {
		return resource;
	}

	public Resource getResource() {
		return resource;
	}

	public void setResource(Resource resource) {
		this.resource = resource;
	}

	/*
	 * public void validate() { if (getResource().getTitle().length() == 0)
	 * addFieldError("title", "Preencha o título."); if
	 * (getResource().getCreator().length() == 0) addFieldError("creator",
	 * "Preencha o autor."); }
	 */

	public String makeCodeMarc() {
		HttpServletRequest request = ServletActionContext.getRequest();

		Integer marcCount = Integer.parseInt(request.getParameter("marcCount"));

		String marcCodes = "";

		for (int i = 1; i <= marcCount; i++) {

			String code = request.getParameter("marcCode" + i);
			String data = request.getParameter("marcData" + i);
			if (code != null && data != null) {
				code = code.trim();
				data = data.trim();
				marcCodes += code + " " + data;
				if (i != marcCount) {
					marcCodes += "\n";
				}
			}

		}
		return marcCodes;
	}

	@SkipValidation
	public String createMarc() {
		Resource resourceAux = getModel();
		resourceAux.setMarcCode(makeCodeMarc());
		if (facade.addResource(resource))
			return SUCCESS;
		return ERROR;

	}

	public String saveMARC() {
		HttpServletRequest request = ServletActionContext.getRequest();
		
		Map<String, String[]> map = new TreeMap<String, String[]>();
		map.putAll(request.getParameterMap());

		Resource r = MarcFormData.getData(map);
		System.out.println(r.getType());
		System.out.println(r.getMarcCode());
		
		setResource(r);
		new ResourceDAO().save(r);
		
		return SUCCESS;
	}
	
	/*
	 * public String toFriendly(){
	 * 
	 * HttpServletRequest request = ServletActionContext.getRequest();
	 * 
	 * Integer marcCount = Integer.parseInt(request.getParameter("marcCount"));
	 * 
	 * 
	 * Resource re = new Resource(); for (int i = 1; i <= marcCount; i++) {
	 * 
	 * String code = request.getParameter("marcCode" + i); String data =
	 * request.getParameter("marcData" + i);
	 * 
	 * if (code != null && data != null) { code = code.trim(); data =
	 * data.trim();
	 * 
	 * if(code.equals("245$a")){ re.setTitle(data); }else
	 * if(code.equals("100$a") ||code.equals("110$a") || code.equals(" 111$a")){
	 * re.setCreator(data); } else if(code.equals("024$a")
	 * ||code.equals("020$a") || code.equals(" 022$a")){ re.setCutter(data);
	 * }else if (code.equals("260$c")) { re.setDate(data); }else
	 * if(code.equals("856$q")){ re.setFormat(data); }else
	 * if(code.equals("700$a") ||code.equals("710$a") || code.equals("711$a")){
	 * re.setFormat(data); }else if(code.equals("786$a")){ re.setSource(data);
	 * }else if(code.equals("080$a")){ re.setCduClassification(data); }else
	 * if(code.equals("600$a") ||code.equals("610$a") || code.equals("611$a")||
	 * code.equals("630$a")|| code.equals("650$a")|| code.equals("651$a")||
	 * code.equals("653$a")){ re.setSubject(data); } }else
	 * if(code.equals("520$a")||code.equals("505$a")){ re.setDescription(data);
	 * }else if(code.equals("655$a")){ // tipo }else if(code.equals("044$a")){
	 * re.setCountry(data); }else if(code.equals("546$a")){
	 * re.setLanguageName(data); }else if(code.equals("260$b")){
	 * re.setPublisherName(data); }else if(code.equals("260$a")){
	 * re.setPublisherPlace(data); }else if(code.equals("250$a")){ // edicao
	 * }else if(code.equals("440$v")){ re.setVolumeNumber(data); }else
	 * if(code.equals("020$a")){ re.setIsbn(data); } else
	 * if(code.equals("022$a")){ re.setIssn(data); }else
	 * if(code.equals("024$a")) { re.setNumber(data); } } setResource(re);
	 * return "edit";
	 * 
	 * }
	 */
}
